package com.ziq.zcamp.main

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import com.amap.api.maps.MapsInitializer
import com.amap.api.services.core.ServiceSettings
import com.ziq.base.base.BaseActivity
import com.ziq.zcamp.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint
import com.ziq.base.extension.binding
import com.ziq.base.utils.LogUtil
import com.ziq.base.utils.SystemBarUtil
import com.ziq.zcamp.R
import com.ziq.zcamp.dialog.PrivacyDialog
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.PrivacyManager

@AndroidEntryPoint
class MainActivity : BaseActivity() {

    companion object {
        const val TAG = "main"
        const val SWITCH_TAB_VALUE = "switch_tab_value"
        fun backToHomeTab(context: Context, tabValue : Int){
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra(SWITCH_TAB_VALUE, tabValue)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
            GlobalContextManager.startActivity(context, intent)
        }
    }

    private val binding : ActivityMainBinding by binding()

    val mViewModel: MainActivityViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        SystemBarUtil.immerseStatusBar(this.window, statusBarIsLight = true)

        LogUtil.i(TAG, "MainActivity onCreate $this")
        binding.root
        if(!PrivacyManager.isPrivacyHasShowed()){
            PrivacyDialog(this,
                onCancel = {
                    PrivacyManager.storePrivacyDialogShowed()
                    onPrivacyAllowed(true)
                },
                onConfirm = {
                    PrivacyManager.storePrivacyDialogShowed()
                    PrivacyManager.storeAgreedPrivacy()
                    onPrivacyAllowed(true)
                }
            ).show()
        } else {
            onPrivacyAllowed(false)
        }
    }

    private fun onPrivacyAllowed(isAfterPrivacyDialog: Boolean){
        if(isAfterPrivacyDialog){
            MapsInitializer.updatePrivacyAgree(GlobalContextManager.appContext, true)
            ServiceSettings.updatePrivacyAgree(GlobalContextManager.appContext,true);
        }
        var targetFragment = supportFragmentManager.findFragmentByTag("MainFragment")
        if(targetFragment == null){
            targetFragment = MainFragment()
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_fragment_container, targetFragment, "MainFragment")
            .commit()
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        switchTab(intent)
    }

    private fun switchTab(intent: Intent?) {
        intent?.getIntExtra(SWITCH_TAB_VALUE, -1)?.let { it ->
            var targetFragment = supportFragmentManager.findFragmentByTag("MainFragment")
            (targetFragment as? MainFragment)?.let { fragment ->
                fragment.switchTab(it)
            }
        }
    }

}